package com.soup.common.module.sys.api;

import com.google.common.collect.ImmutableMap;
import com.soup.common.entity.ErrorCode;
import com.soup.common.entity.QueryParam;
import com.soup.common.entity.QueryRequest;
import com.soup.common.entity.Result;
import com.soup.common.exception.sys.DictionaryException;
import com.soup.common.module.sys.data.DictRequest;
import com.soup.common.module.sys.entity.Dictionary;
import com.soup.common.module.sys.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统数据字典接口
 *
 * @author zhaoyi
 * @date 2019-08-04 14:22
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/dictionary")
public class DictionaryApi {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 分页查询字典，支持字典key和字典类型名称模糊查询
     *
     * @param queryParam 分页查询参数
     * @return 分页字典信息
     */
    @GetMapping
    public Result page(@Valid QueryRequest queryParam) {
        return dictionaryService.page(queryParam);
    }

    /**
     * 根据字典类型查询字典列表
     *
     * @param dictType 字典类型
     * @return 字典列表
     */
    @GetMapping("/{dictType}")
    public List<Dictionary> findByType(@PathVariable String dictType) {
        return dictionaryService.selectByEqual(ImmutableMap.of("dictType", dictType));
    }

    /**
     * 新增字典
     *
     * @param dictRequest 新增字典入参对象
     * @return 新增结果
     */
    @PostMapping
    public int addDic(@RequestBody @Valid DictRequest dictRequest) {
        // 校验新增的父级字典分类是否存在
        int count = dictionaryService.countByEqual(ImmutableMap.of("dictName", dictRequest.getDictName()));
        if (count > 0) {
            throw new DictionaryException(ErrorCode.DICT_ADD_DUPLICATED);
        }

        Dictionary dictionary = new Dictionary();
        BeanUtils.copyProperties(dictRequest, dictionary);
        return dictionaryService.save(dictionary);
    }

    /**
     * 修改字典
     *
     * @param dictId 字典id
     * @param dictRequest 字典信息
     * @return 操作结果
     */
    @PutMapping("/{dictId}")
    public int edit(@PathVariable Integer dictId, @RequestBody @Valid DictRequest dictRequest) {
        Dictionary dictionary = dictionaryService.selectByKey(dictId);
        if (null == dictionary) {
            throw new DictionaryException(ErrorCode.DICT_NOT_FOUND);
        }

        // 判断字典名称是否存在
        QueryParam queryParam = new QueryParam(Dictionary.class);
        QueryParam.Criteria criteria = queryParam.createCriteria();
        criteria.andEqualTo("dictName", dictRequest.getDictName());
        // 更新判断是否重复
        criteria.andNotEqualTo("id", dictId);
        List<Dictionary> dics = dictionaryService.selectByQueryParam(queryParam);
        if (CollectionUtils.isNotEmpty(dics)) {
            throw new DictionaryException(ErrorCode.DICT_ADD_DUPLICATED);
        }

        BeanUtils.copyProperties(dictRequest, dictionary);
        return dictionaryService.updateNotNull(dictionary);
    }

    /**
     * 删除字典
     *
     * @param dictId 字典主键
     * @return 删除结果
     */
    @DeleteMapping("/{dictId}")
    public int delete(@PathVariable Integer dictId) {
        Dictionary dictionary = dictionaryService.selectByKey(dictId);
        if (null == dictionary) {
            throw new DictionaryException(ErrorCode.DICT_NOT_FOUND);
        }
        return dictionaryService.delete(dictId);
    }
}
